﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace CSharp.Common.StringOperation
{
    public class HtmlDocumentHelper
    {
        public static string CutHtmlSource(string str)
        {
            StringBuilder builder = new StringBuilder(str);
            int index = str.IndexOf("<head");
            int num2 = str.IndexOf("</head>");
            builder.Remove(index, (num2 - index) + "</head>".Length);
            return CutHtmlSource(CutHtmlSource(CutHtmlSource(CutHtmlSource(CutHtmlSource(CutHtmlSource(CutHtmlSource(builder.ToString(), "<script", "</script>"), "<noscript", "</noscript>"), "<iframe", "</iframe>"), "<style", "</style>"), "<!--", "-->"), "<link", "</>"), "<img", "/>");
        }

        public static string CutHtmlSource(string source, string start, string end)
        {
            string str = "";
            StringBuilder builder = new StringBuilder(source);
            int index = source.IndexOf(start);
            for (int i = source.IndexOf(end); ((index != -1) && (i != -1)) && (index < i); i = str.IndexOf(end))
            {
                builder.Remove(index, (i - index) + end.Length);
                str = builder.ToString();
                index = str.IndexOf(start);
            }
            return ((str == "") ? source : str);
        }

        public static string GetHtml(string start, string end, string html)
        {
            string str = string.Empty;
            try
            {
                string pattern = string.Format("{0}(?<g>(.|[\r\n])+?)?{1}", start, end);
                str = Regex.Match(html, pattern).Groups["g"].Value;
            }
            catch
            {
            }
            return str;
        }

        public static List<string> GetHtmls(string start, string end, string html)
        {
            List<string> list = new List<string>();
            try
            {
                string pattern = string.Format("{0}(?<g>(.|[\r\n])+?){1}", start, end);
                MatchCollection matchs = Regex.Matches(html, pattern);
                if (matchs.Count == 0)
                {
                    return list;
                }
                foreach (Match match in matchs)
                {
                    GroupCollection groups = match.Groups;
                    list.Add(groups["g"].Value);
                }
            }
            catch
            {
            }
            return list;
        }

        public static string GetInnerTagString(string tagName, string innerTagName, string result, string yinhao)
        {
            string str = "";
            string pattern = "";
            if (yinhao == "single")
            {
                pattern = "<" + tagName + "[^>]*?" + innerTagName + "='(?<url>[^\"]*)'[^>]*>";
            }
            else
            {
                pattern = "<" + tagName + "[^>]*?" + innerTagName + "=\"(?<url>[^\"]*)\"[^>]*>";
            }
            Match match = Regex.Match(result, pattern, RegexOptions.IgnoreCase);
            if (match.Success)
            {
                str = match.Groups["url"].Value.Replace("'", "");
            }
            return str;
        }

        public static string GetTagInnerHtml(string tagName, string source)
        {
            int index = source.IndexOf("<" + tagName, StringComparison.CurrentCultureIgnoreCase);
            int num2 = source.IndexOf("</" + tagName + ">", StringComparison.CurrentCultureIgnoreCase);
            if (index < 0)
            {
                return "";
            }
            string str = source.Substring(index, ((num2 - index) + tagName.Length) + 3);
            int num3 = str.IndexOf(">", StringComparison.CurrentCultureIgnoreCase);
            int num4 = str.IndexOf("</" + tagName + ">", StringComparison.CurrentCultureIgnoreCase);
            if (num3 < 0)
            {
                return "";
            }
            return str.Substring(num3 + 1, (num4 - num3) - 1);
        }

        public static string GetTagInnerValue(string tagName, ref string source)
        {
            int index = source.IndexOf("<" + tagName);
            if (index != -1)
            {
                source = source.Substring(index);
            }
            int startIndex = source.IndexOf("<" + tagName, StringComparison.CurrentCultureIgnoreCase);
            int num3 = source.IndexOf("</" + tagName + ">", StringComparison.CurrentCultureIgnoreCase);
            if (startIndex < 0)
            {
                return "";
            }
            string str = source.Substring(startIndex, ((num3 - startIndex) + tagName.Length) + 3);
            int num4 = str.IndexOf(">", StringComparison.CurrentCultureIgnoreCase);
            int num5 = str.IndexOf("</" + tagName + ">", StringComparison.CurrentCultureIgnoreCase);
            if (num4 < 0)
            {
                return "";
            }
            source = source.Substring((num3 + tagName.Length) + 3, ((source.Length - num3) - tagName.Length) - 3);
            return str.Substring(num4 + 1, (num5 - num4) - 1);
        }

        public static MatchCollection GetTags(string tagName, string result)
        {
            Regex regex = new Regex("<" + tagName + "\\s(?:[^\"'><]*|\"[^\"]*\"|'[^']*')*>(?:(?!</" + tagName + @">)[\s\S])+</" + tagName + ">", RegexOptions.IgnoreCase);
            return regex.Matches(result);
        }

        public static MatchCollection GetTagsNotHtml(string tagNameA, string tagNameB, string result)
        {
            return Regex.Matches(result, "<" + tagNameA + ".*?" + tagNameB + "=\"(?<img>[^\"]*)\"[^>]*>", RegexOptions.IgnoreCase);
        }
    }
}

